import java.io.IOException;
import java.util.Arrays;

public class BinarySearch {

	private int binarySearch(String[] arr, String key) {
		int low = 0;
		int high = arr.length - 1;

		while (low <= high) {
			int mid = (low + high) >>> 1;
			String midVal = arr[mid];
			int cmp = midVal.compareTo(key);

			if (cmp < 0)
				low = mid + 1;
			else if (cmp > 0)
				high = mid - 1;
			else
				return mid; // key found
		}
		return -(low + 1); // key not found.
	}

	public static void main(String args[]) throws IOException {
		System.out.println("Welcome to the Binary search tree.. ");
		System.out.println("Enter the number of elements :");

		String[] arr = new String[] { "Ramesh", "ramesh", "rajan", "anuj",
				"Annu" };
		Arrays.sort(arr);
		for (String s : arr) {
			System.out.println(s);
		}
		BinarySearch srch = new BinarySearch();
		int i = srch.binarySearch(arr, args[0]);
		System.out.println(i);
	}

}
